package cn.wubo.swagger;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2 //开swagger
public class SwaggerConfiguration {

    private static String model = "4";

    @Bean
    public Docket api() {
        switch (model) {
            case "1":
                return new Docket(DocumentationType.SWAGGER_2)
                        .select()
                        //allows selection of RequestHandler's using a predicate. The example here uses an any predicate (default). Out of the box predicates provided are any, none, withClassAnnotation, withMethodAnnotation and basePackage.
                        .apis(RequestHandlerSelectors.any())
                        //allows selection of Path's using a predicate. The example here uses an any predicate (default). Out of the box we provide predicates for regex, ant, any, none.
                        .paths(PathSelectors.any())
                        .build()
                        //是否开启
                        .enable(true)
                        //Adds a servlet path mapping, when the servlet has a path mapping. This prefixes paths with the provided path mapping.
                        .pathMapping("/");
            case "2":
                return new Docket(DocumentationType.SWAGGER_2)
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("cn.wubo.swagger.controller"))
                        .paths(PathSelectors.ant("/testSwagger/**"))
                        .build()
                        .enable(true)
                        .pathMapping("/");
            case "3":
                return new Docket(DocumentationType.SWAGGER_2)
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("cn.wubo.swagger.controller"))
                        .paths(PathSelectors.ant("/testSwagger/**"))
                        .build()
                        .enable(true)
                        .pathMapping("/")
                        .apiInfo(apiInfo());
            case "4":
                return new Docket(DocumentationType.SWAGGER_2)
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("cn.wubo.swagger.controller"))
                        .paths(Predicates.or(PathSelectors.ant("/testSwagger/**"),PathSelectors.ant("/book/**")))
                        .build()
                        .enable(true)
                        .pathMapping("/")
                        .apiInfo(apiInfo())
                        //Flag to indicate if default http response codes need cto be used or not.
                        .useDefaultResponseMessages(true)
                        //Allows globally overriding response messages for different http methods.
                        .globalResponseMessage(RequestMethod.POST, responseMessagePostList());
            default:
                return new Docket(DocumentationType.SWAGGER_2)
                        .select()
                        .apis(RequestHandlerSelectors.any())
                        .paths(PathSelectors.any())
                        .build();
        }
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "REST API DEMO",
                "可以放一些描述",
                "0.1",
                "none",
                new Contact("wubo", "https://github.com/wb04307201/SpringBoot", "wubo_qm@faw.com.cn"),
                "License of API", "API license URL", Collections.emptyList());
    }

    private List<ResponseMessage> responseMessagePostList() {
        return new ArrayList<ResponseMessage>() {
            {
                add(new ResponseMessageBuilder()
                        .code(403)
                        .message("403 message Forbidden")
                        .build());
                add(new ResponseMessageBuilder()
                        .code(404)
                        .message("404 message Not Found")
                        .build());
            }
        };
    }
}
